﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SlamCms.Data.Repositories;
using Microsoft.SharePoint;
using System.Linq;
using SlamCms.SharePoint.Core;
using SlamCms.Configuration;
using SlamCms.Data;

namespace SlamCms.SharePoint.Publishing.WebParts
{
    public partial class CommentsCounterControl : UserControl
    {

        

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ltCommentsManagerUrl.Text = SPContext.Current.Web.Url.TrimEnd('/') + "/" + ConfigurationManager.Solution.Settings["CommentsManagerUrl"].TrimStart('/');
            //this.ltCommentsManagerUrl.Text = SPContext.Current.Web.Url.TrimEnd('/') + "/" + ManagerUrl.TrimStart('/');
            LoadControls();

        }

        private void LoadControls()
        {
            try
            {
				CommentRepository repository = new CommentRepository(new DefaultDbConnectionResolver().GetConnection());

                var result = repository.GetCommentsCount(SPContext.Current.Web.ID).FirstOrDefault();

                if (result.Comments > 0)
                {
                    this.ltTotal.Text = result.Comments.ToString();
                    this.ltLastSubmitted.Text = result.LastSubmitted.ToShortDateString();
                }
            }
            catch (Exception)
            { }

        }
    }
}
